package com.five.controller;

import com.alibaba.fastjson.JSON;
import com.five.entity.SchoolActivities;
import com.five.service.FileGroupService;
import com.five.service.SchoolActivitiesService;
import com.five.service.UserBusinessActivitiesService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zero on 2018/2/26.
 */
@RestController
@RequestMapping("/youxue/index")
public class SchoolActivitiesController {

    private static final Logger looger = Logger.getLogger(SchoolActivitiesController.class);


    @Autowired
    private SchoolActivitiesService schoolActivitiesService;
    @Autowired
    private UserBusinessActivitiesService userBusinessActivitiesService;
    @Autowired
    private FileGroupService fileGroupService;


    /**
     * 查找不同类型的学校活动信息
     * @return
     */
    @RequestMapping(value = "/school/{activitiesType}",method = RequestMethod.GET,produces ="text/plain;charset=UTF-8")
    public String getSchoolActivities(@PathVariable String activitiesType){

        Map<String,Object> map= new HashMap<String, Object>();
        List<SchoolActivities> schoolActivities=schoolActivitiesService.findSchoolActivitiesByActivitiesType(activitiesType);
        int[] activitiesNumbers=new int[schoolActivities.size()];
        if(schoolActivities!=null){
            for(int i=0;i<schoolActivities.size();i++){
                activitiesNumbers[i]=userBusinessActivitiesService.findGroupPersonNumber(schoolActivities.get(i).getId());
                String img=fileGroupService.findFileGroupById(schoolActivities.get(i).getActivityFileGroupId()).getGroupBaseUrl();
                schoolActivities.get(i).setActivityImg(img);
            }
            map.put("schoolActivities",schoolActivities);
            map.put("activitiesNumbers",activitiesNumbers);
            return JSON.toJSON(map).toString();

        }else {
            return "error";
        }
    }


    /**
     * 添加活动
     * @param schoolActivities
     * @return
     */
    @RequestMapping(value = "/school/add",method = RequestMethod.POST)
    public String addActivities(@RequestBody SchoolActivities schoolActivities) {

        if(schoolActivities!=null){
            schoolActivitiesService.addSchoolActivities(schoolActivities);
            return "add success";
        }else {
            return "error";
        }
    }


    /**
     * 获取最新活动
     * @return
     */
    @RequestMapping(value = "/school/newest",method = RequestMethod.GET,produces ="text/plain;charset=UTF-8")
    public String getNewActivities(){

        Map<String,Object> map= new HashMap<String, Object>();
        List<SchoolActivities> schoolActivities=schoolActivitiesService.findNewActivitiesByNumber(0,10);
        int[] activitiesNumbers=new int[schoolActivities.size()];
        if(schoolActivities!=null){
            for(int i=0;i<schoolActivities.size();i++){
                activitiesNumbers[i]=userBusinessActivitiesService.findGroupPersonNumber(schoolActivities.get(i).getId());
                String img=fileGroupService.findFileGroupById(schoolActivities.get(i).getActivityFileGroupId()).getGroupBaseUrl();
                schoolActivities.get(i).setActivityImg(img);
            }
            map.put("schoolActivities",schoolActivities);
            map.put("activitiesNumbers",activitiesNumbers);
            return JSON.toJSON(map).toString();
        }else {
            return "error";
        }
    }

}
